package com.todo.event.activity.application.handler.activityrule;

import com.alibaba.fastjson.JSONObject;
import com.todo.event.activity.application.entity.context.ActivityRuleContext;
import lombok.extern.slf4j.Slf4j;

/**
 * @author azhebuxing
 * @date 2025/1/13 13:36
 * @description
 */
@Slf4j
public abstract class AbstractActivityRuleHandle {

    protected AbstractActivityRuleHandle successor = null;

    public void setSuccessor(AbstractActivityRuleHandle successor) {
        this.successor = successor;
    }

    public void ruleHandle(ActivityRuleContext context) {
        boolean result = true;
        if (ruleMatch(context)) {
            //匹配当前规则，做对应处理
            result = doRuleHandle(context);
            log.info("AbstractActivityRuleHandle#handle 匹配到规则，开始处理，规则名称：【{}】,入参：【{}】,执行结果:【{}】", ruleName(), JSONObject.toJSONString(context), result);
            if (successor != null && result) {
                successor.ruleHandle(context);
            }
            if (!result) {
                context.setExecuteResult(false);
            }
        }
    }

    /**
     * 规则名称
     *
     * @return
     */
    protected abstract String ruleName();

    protected abstract boolean doRuleHandle(ActivityRuleContext context);

    protected abstract boolean ruleMatch(ActivityRuleContext context);
}
